/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.main.action;

import com.documents.common.core.PageBean;
import com.documents.common.core.action.BaseActionSupport;
import com.documents.pojo.Message;
import com.documents.pojo.Users;
import com.documents.service.MessageService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Administrator
 */
public class MessageAction extends BaseActionSupport implements ServletRequestAware {
    
    private Message msg;
    private MessageService messageService;
    private int msg_id;
    private int page=1;
    private PageBean pageBean;
    private int pageSize = 20;
    private String authorName;
    private String content;
    
    public MessageService getMessageService() {
        return messageService;
    }
    
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }
    
    public String getContent() {
        return content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    public Message getMsg() {
        return msg;
    }
    
    public void setMsg(Message msg) {
        this.msg = msg;
    }
    
    public int getMsg_id() {
        return msg_id;
    }
    
    public void setMsg_id(int msg_id) {
        this.msg_id = msg_id;
    }
    
    public String getAuthorName() {
        return authorName;
    }
    
    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }
    
    public int getPage() {
        return page;
    }
    
    public void setPage(int page) {
        this.page = page;
    }
    
    public PageBean getPageBean() {
        return pageBean;
    }
    
    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }
    
    public int getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    
    public Message getMessage() {
        return msg;
    }
    
    public void setMessage(Message msg) {
        this.msg = msg;
    }
    
    public int getMessage_id() {
        return msg_id;
    }
    
    public void setMessage_id(int msg_id) {
        this.msg_id = msg_id;
    }

    // 1.增;
    public void addMessage() throws Exception {
        String data = "fail";
        if (msg != null) {
            msg.setCreatetime(new Date());
            messageService.save(msg);
            data = "success";
        }
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            PrintWriter out = response.getWriter();
            out.print(data);
        } catch (IOException e) {
        }
        
    }
    
    public String delete() throws Exception {
        
        messageService.delById(msg.getId());
        return SUCCESS;
    }
    
    public String updateMessage() throws Exception {
        
        if (msg.getRecontent() != null && !msg.getRecontent().equals("")) {
            Message msgtemp = messageService.get(msg.getId());
            msgtemp.setRecontent(msg.getRecontent());
            msgtemp.setState(1);
           
            msgtemp.setRecreatetime(msg.getRecreatetime());
            messageService.update(msgtemp);
            return SUCCESS;
        } else {
            msg = messageService.get(msg.getId());
            return ERROR;
        }
        
        
    }
    
    public String show() throws Exception {
        if (msg.getId() > 0) {
            msg = messageService.get(msg.getId());
            return SUCCESS;
        } else {
            return ERROR;
        }
        
        
    }
    
    public String list() throws Exception {
        pageBean = messageService.getPageBean(page, pageSize, msg);
        
        return SUCCESS;
    }
    
    public String addInput() throws Exception {
        Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
        if (users != null) {
            authorName = users.getName();
        }
        return SUCCESS;
    }
    
    public String updateInput() throws Exception {
        Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
        if (users != null) {
            authorName = users.getName();
        }
        if (msg.getId() > 0) {
            msg = messageService.get(msg.getId());
            return SUCCESS;
        } else {
            return null;
        }
        
    }
}
